package IO流.综合练习.随机点名;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class 第四题 {
    public static void main(String[] args) throws IOException {
        //1.创建文件的输出流对象
        BufferedReader nameBf=new BufferedReader(new FileReader("F\\name.txt"));
        BufferedReader countBf=new BufferedReader(new FileReader("F\\count.txt"));
        BufferedReader lastNameBf=new BufferedReader(new FileReader("F\\lastname.txt"));

        //2.把文件中的数据添加到集合中
        //创建一个集合存储学生的姓名
        ArrayList<String> nameList=new ArrayList<>();
        //创建一个集合存储已经点过名的学生
        ArrayList<String> lastNameList=new ArrayList<>();
        String name,lastname;
        while ((name=nameBf.readLine())!=null){
            nameList.add(name);
        }
        while ((lastname=lastNameBf.readLine())!=null){
            lastNameList.add(lastname);
        }
        //3.打乱点名的集合直到找到没有点过名的集合
        while (true){
            //如果点过名的列表长的为10,表示都点过名了直接结束
            if (lastNameList.size()==10){break;}
            Collections.shuffle(nameList);
            //如果在打乱出了在点过名的列表中不存在的名字,结束循环
            if (!lastNameList.contains(nameList.get(0))){
                break;
            }
        }
        //4.判断此时listnameList集合长度是否到达10
        if (lastNameList.size()==10){
            System.out.println("开始下一轮");
            //清空lastname.txt文件
            FileWriter fw=new FileWriter("F\\lastname.txt");
            fw.close();
        }
        //5.开始点名
        //转换记录的次数为int类型
        int count = Integer.parseInt(countBf.readLine());
        count++;
        //创建记录文件的输出流
        BufferedWriter bw=new BufferedWriter(new FileWriter("F\\lastname.txt",true));
        BufferedWriter countBw=new BufferedWriter(new FileWriter("F\\count.txt"));
        //写入已经点名的名字
        bw.write(nameList.get(0));
        bw.newLine();
        System.out.println("程序第"+count+"次运行:"+nameList.get(0));
        countBw.write(String.valueOf(count));
        //释放资源
        countBw.close();
        bw.close();

        //6.释放资源
        lastNameBf.close();
        countBf.close();
        nameBf.close();
    }
}
